package org.edu.modules.sxjy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Objects;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeResult;
import org.edu.modules.sxjy.service.IVeResultService;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.vo.VeResultEvaluationVo;
import org.edu.modules.sxjy.vo.VeResultInfoVo;
import org.edu.modules.sxjy.vo.VeResultVo;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Api(tags={"实习成绩"})
@RestController
@RequestMapping({"/sxjy/veResult"})
@Component
@ApiSort(60)
public class VeResultController
        extends BaseController<VeResult, IVeResultService>
{
    private static final Logger log = LoggerFactory.getLogger(VeResultController.class);
    @Autowired
    private IVeResultService veResultService;

    @AutoLog("实习成绩-导出数据")
    @ApiOperation(value="导出数据", notes="实习成绩-导出数据")
    @PostMapping({"/exportXls"})
    public ModelAndView exportXls(@RequestBody VeCommon veCommon)
    {
        List<VeResultVo> veResultVoList = this.veResultService.selectAlls(veCommon);

        ModelAndView modelAndView = new ModelAndView(new JeecgEntityExcelView());

        modelAndView.addObject("fileName", "实习结果信息");
        modelAndView.addObject("entity", VeResultVo.class);
        LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        modelAndView.addObject("params", new ExportParams("实习结果列表", "导出人:" + user.getRealname(), "导出信息"));
        modelAndView.addObject("data", veResultVoList);
        return modelAndView;
    }

    @AutoLog("实习成绩-查询列表")
    @ApiOperation(value="查询列表", notes="实习成绩-查询列表")
    @GetMapping({"/selectAll"})
    public PageResponseResult selectAll(VeResultVo veResultVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, VeCommon veCommon)
    {
        Integer startLine = Integer.valueOf((pageNo.intValue() - 1) * pageSize.intValue());
        List<VeResultVo> veResultVoList = this.veResultService.selectAll(veResultVo, startLine, pageSize, veCommon);
        int total = this.veResultService.getSumPage(veResultVo, veCommon);
        if ((Objects.isNull(veResultVoList)) && (veResultVoList.size() <= 0)) {
            return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.OK());
        }
        double totals = total;
        double pageSizes = pageSize.intValue();
        double totalPage = Math.ceil(totals / pageSizes);
        return new PageResponseResult(pageNo, (int)totalPage, total, Result.OK(veResultVoList));
    }

    @AutoLog("实习成绩-查看详细")
    @ApiOperation(value="查看详细", notes="实习成绩-查看详细")
    @GetMapping({"/selectOneInfo"})
    public Result<?> selectOneInfo(int id, String userType, String userId)
    {
        VeResultInfoVo veResultInfoVos = this.veResultService.getOneInfo(id, userType, userId);
        if (veResultInfoVos != null) {
            return Result.OK(veResultInfoVos);
        }
        return Result.OK("未找到对应数据");
    }

    @AutoLog("实习成绩-学生成绩评定回显")
    @ApiOperation(value="学生成绩评定回显", notes="实习成绩-学生成绩评定回显")
    @GetMapping({"/stuGradeEvaluationEcho"})
    public Result<?> stuGradeEvaluationEcho(int id)
    {
        if (id != 0)
        {
            VeResultEvaluationVo veResultEvaluationVo = this.veResultService.evaluationEcho(id);
            return Result.OK(veResultEvaluationVo);
        }
        return Result.OK("回显失败");
    }

    @AutoLog("实习成绩-学生成绩评定")
    @ApiOperation(value="学生成绩评定", notes="实习成绩-学生成绩评定")
    @PutMapping({"/stuGradeEvaluation"})
    public Result<?> stuGradeEvaluation(@RequestBody VeResultEvaluationVo veResultEvaluationVo)
    {
        if (veResultEvaluationVo != null) {
            return this.veResultService.stuGradeEvaluation(veResultEvaluationVo);
        }
        return Result.error("编辑失败!");
    }
}
